package com.dingwei.lesyndrome.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.app.MyApplication;
import com.dingwei.lesyndrome.bean.GamCircleListBean;
import com.dingwei.lesyndrome.bean.MyNewsBean;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.SpaceDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DisplayUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created time 2018/3/2.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class GamCircleListAdapter extends RecyclerArrayAdapter<GamCircleListBean.DataBean.ListBean> {

    public interface clickInterFace {
        void  onLike(int position);
        void  onComment(int position);
        void  onPlay(int position);
        void  onHead(int position);
    }
    private clickInterFace clickInterfaceListener;
        public void xxInterfaceListener(clickInterFace listener){
            clickInterfaceListener = listener;

        }



    public GamCircleListAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new GamCircleListHolder(parent);
    }

    public class GamCircleListHolder extends BaseViewHolder<GamCircleListBean.DataBean.ListBean> {


        @Bind(R.id.img_head)
        ImageView imgHead;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.tv_content)
        TextView tvContent;
        @Bind(R.id.img_top)
        ImageView imgTop;
        @Bind(R.id.fl_video)
        FrameLayout flVideo;
        @Bind(R.id.tv_watch)
        TextView tvWatch;
        @Bind(R.id.tv_address)
        TextView tvAddress;
        @Bind(R.id.img_like)
        ImageView imgLike;
        @Bind(R.id.img_comment)
        ImageView imgComment;
        @Bind(R.id.grid_pics)
        RecyclerView gridPics;

        GamCirclePicAdapter adapter;

        public GamCircleListHolder(ViewGroup parent) {
            super(parent, R.layout.item_gam_circle);
            ButterKnife.bind(this, itemView);

            adapter = new GamCirclePicAdapter(BaseActivity.context);
            GridLayoutManager layoutManager = new GridLayoutManager(BaseActivity.context, 4);
            layoutManager.setSpanSizeLookup(adapter.obtainGridSpanSizeLookUp(4));
            SpaceDecoration itemDecoration = new SpaceDecoration((int) DisplayUtil.dip2px(BaseActivity.context, 10));//参数是距离宽度
            itemDecoration.setPaddingEdgeSide(false);//是否为左右2边添加padding.默认true.
            itemDecoration.setPaddingStart(false);//是否在给第一行的item添加上padding(不包含header).默认true.
            itemDecoration.setPaddingHeaderFooter(false);//是否对Header于Footer有效,默认false.
            gridPics.setLayoutManager(layoutManager);
            gridPics.setAdapter(adapter);
            gridPics.setNestedScrollingEnabled(false);
            gridPics.addItemDecoration(itemDecoration);
        }

        @Override
        public void setData(GamCircleListBean.DataBean.ListBean data) {
            super.setData(data);
            Glide.with(BaseActivity.context)
                    .load(data.showHportrait)
                    .apply(MyApplication.headOptions)
                    .into(imgHead);
            tvName.setText(data.showName+"");
            tvTime.setText(data.showTime);
            tvContent.setText(data.content);
            tvWatch.setText("浏览"+data.views+"次");
            tvAddress.setText(data.address);

            if (TextUtils.isEmpty(data.video)) {
                flVideo.setVisibility(View.GONE);
            }else {
                flVideo.setVisibility(View.VISIBLE);
                Glide.with(BaseActivity.context)
                        .load(data.vimg)
                        .apply(MyApplication.options_no_transform)
                        .into(imgTop);
            }

            if (data.imgList!=null&&data.imgList.size()>0){
                gridPics.setVisibility(View.VISIBLE);
                adapter.clear();
                for (int i = 0; i < data.imgList.size(); i++) {
                    adapter.add(data.imgList.get(i).showFile);
                }
            }else {
                gridPics.setVisibility(View.GONE);
            }
            if (data.isAgree){
                imgLike.setImageResource(R.drawable.ic_circle_like);
            }else {
                imgLike.setImageResource(R.drawable.ic_circle_normal);
            }

            imgLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickInterfaceListener!=null){
                        clickInterfaceListener.onLike(getDataPosition());
                    }
                }
            });
            flVideo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickInterfaceListener!=null){
                        clickInterfaceListener.onPlay(getDataPosition());
                    }
                }
            });
            imgHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickInterfaceListener!=null){
                        clickInterfaceListener.onHead(getDataPosition());
                    }
                }
            });

        }
    }
}
